<?php

namespace App\Console\Commands;

use App\Models\ElectricCollector;
use App\Models\ElectricElectricity;
use App\Models\MqttModel;
use Illuminate\Console\Command;

class AddEquipment extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:addEquipment
                           {collect_code:采集器编码}
                           {device_type:设备类型 1,电表;2,水表}
                           {start:开始位置}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '添加设备命令';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // 指令输出
        [$collect_code, $str] = $this->assemble();
        try {
            $topic = "server/{$collect_code}";
            $result = (new MqttModel())->sendToDevice($str, $topic);
            $this->info(json_encode($result, JSON_UNESCAPED_UNICODE));
        } catch (\Exception $e) {
            $this->error($e->getMessage(), JSON_UNESCAPED_UNICODE);
        }
    }

    /**
     * 2021/3/17--18:19
     * @effect:拼装数据
     * @return array
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:assemble
     */
    protected function assemble(): ?array
    {
        $args = $this->getArguments();
        $collect_code = $args['collect_code'];
        $collector = ElectricCollector::selectRaw('id,code')->where(['code' => $collect_code])->find();
        if (empty($collector)) {
            $var = ['code' => '100', 'msg' => "采集器不存在,编码：{$collect_code}"];
            $this->error(json_encode($var, JSON_UNESCAPED_UNICODE));
        } else {
            $collect_id = $collector['id'];
            $where = [
                'status' => 1,
                'collect_id' => $collect_id,
            ];
            $ele_arr = ElectricElectricity::selectRaw('cate,tableAddress code')
                ->where($where)
                ->limit($args['start'], 20)
                ->select();
            if (!$ele_arr->count) {

                $var1 = ['code' => '100', 'msg' => '暂无设备需要添加'];
                $this->error(json_encode($var1, JSON_UNESCAPED_UNICODE));
            } else {
                $ele_arr = $ele_arr->toArray();
                $now_count = count($ele_arr);
                $ele_count = ElectricElectricity::where($where)
                    ->count();
                $args['start'] += 20;
                $str = [
                    'sync' => 'true',//同步标识
                    'syncId' => 'server/9999',//同步消息号
                    'ver' => 1,//消息版本号
                    'id' => $args['collect_code'],//表号
                    'devVer' => 2,//设备版本号
                    'type' => 6,//绑定消息
                    'flag' => 'one',
                    'data' => [
                        'arr_count' => $ele_count,
                        'now_count' => $now_count,
                        'start' => $args['start'],
                        'device_type' => $args['device_type'],
                        'arr' => $ele_arr,
                        'now' => time(),
                    ]//数据区
                ];
                return array($collect_code, $str);
            }
        }
    }
}
